// pages/studyWord/ninebox/ninebox.js
const app = getApp();
const httpUtils = require('../../../utils/httpUtils.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: app.globalData.imageurl,
    roundnum: 0, //学习轮数
    course_type: 0, //0是体验词库 1正式词库
    packageid: "",
    course: "请选择词库",
    boxsStatus: [],
    isShow: false, //true 对话框出现
    dialogCancelText: "休息一下", //取消按钮的文字
    dialogSureText: "开始检查", //确定按钮的文字
    dialogContent: "您有个待复习单词，点击去复习~", //对话框的提示内容
    dialogImgurl: "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/jiugong_pop_daifuxi@2x.png", //对话框的图片url
    dialogIsShowCancel: false,//对话框是否显示取消按钮 true 显示 false 取消
    dialogType: 1,//1去识记2去检查3开启下一轮4:三轮学习完毕 5:确定返回 ,6需复习
    studydata: "",//穿的参数 单词列表
    animstate: 'paused', //点击start各宫格动画状态 开始/暂停
    studyModel: 1,  //0入门  1  标准  2  学霸 study_model==2的是可复习 不等于2是列表展示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCurrentCourse();
    console.log("student_id=" + JSON.stringify(app.globalData.studentInfo.id));
    wx.enableAlertBeforeUnload({
      message: "返回上页时弹出对话框1212",
      success: function (res) {
        console.log("方法注册成功：", res);
      },
      fail: function (errMsg) {
        console.log("方法注册失败：", errMsg);
      },
    });
  },

  //请求获取当前词库
  getCurrentCourse: function () {
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getCurrentCourse({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
    }).then((res) => {
      console.log(res);
      if (res.data.id != 0) {
        that.setData({
          packageid: res.data.id,
          roundnum: res.data.complete,
          course: res.data.level_title,
          course_type: res.data.course_type,
        })
        this.getBoxsStatus(res.data.id);
        //获取到包id就开始计时
        // this.startTime();
      }

    });
  },
    //开始计时
    startTime: function () {
      let that = this;
      httpUtils.startTime({
        access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
        package_id: that.data.packageid,
      }).then((res) => {
  
      });
    },
      //结束计时
      stopTime: function () {
        let that = this;
        httpUtils.stopTime({
          access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
          package_id: that.data.packageid,
        }).then((res) => {
    
        });
      },
  //请求获取九宫格状态
  getBoxsStatus: function () {
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getBoxsStatus({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: this.data.packageid,
    }).then((res) => {
      console.log(res);
      that.setData({
        boxsStatus: res.data.square_total,
        studyModel: res.data.study_model
      })
      console.log("学习模式为：" + that.data.studyModel)
    });

  },
  //开始下一轮
  startnext: function () {
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.startnext({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: this.data.packageid,
    }).then((res) => {
      console.log(res);
      // String mUrl = BuildConfig.API_URL + "/index/index/certificate?complete=" + wordpackagestatusDataBaseRet.data.complete + "&package_id=" + mPackageId + "&student_id=" + MyApp.getInstance().mLoginData.id;
      //结业报告
      let url = app.globalData.apiBaseUrl + "index/index/certificate?complete=" + res.data.complete + "&package_id=" + that.data.packageid + "&student_id=" + app.globalData.studentInfo.id;
      let urlStr = encodeURIComponent(url);
      //成功则跳结业报告界面
      wx.navigateTo({
        url: '/pages/web/web?url=' + urlStr,
      })

    });
  },

  //请求获取选词数据 点start请求的
  getSelectData: function () {
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getSelectData({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: this.data.packageid,
      page: 1,
    }).then((res) => {
      let type = res.data.type;
      that.data.studydata = JSON.stringify(res.data.to_study);
      if (type == 1) {
        //跳选词界面
        wx.navigateTo({
          url: '/pages/studyWord/selectWord/selectWord?packageid=' + that.data.packageid + '&studyData=' + that.data.studydata,
        })
      } else if (type == 11016) { //有未完成的学后检测，请先完成检测
        let num = res.data.to_study.length;
        that.setData({
          isShow: true,
          dialogType: 2,
          dialogContent: "您还有" + num + "个待检查的单词，点击去检查~",
          dialogSureText: "开始检查",
          dialogCancelText: "取消",
          dialogIsShowCancel: true,
          dialogImgurl: "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/jiugong_pop_daijiancha@2x.png",
        });

      } else if (type == 11017) { //有未完成的单词识记，请先完成识记
        that.setData({
          isShow: true,
          dialogType: 1,
          dialogContent: "您上次选择的一篇单词还没背完，先去背完它吧~",
          dialogSureText: "继续识记",
          dialogCancelText: "取消",
          dialogIsShowCancel: true,
          dialogImgurl: "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/jiugong_pop_weibeiwan@2x.png",
        });
      } else if (type == 11008) {
        that.setData({
          isShow: true,
          dialogType: 6,
          dialogContent: res.data.detail,
          dialogSureText: "开始复习",
          dialogCancelText: "继续学习",
          dialogIsShowCancel: true,
          dialogImgurl: "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/jiugong_pop_weibeiwan@2x.png",
        });
      } else if (type == 11040) { //下一轮 
        that.setData({
          isShow: true,
          dialogType: 3,
          dialogContent: res.data.detail,
          dialogSureText: "马上开始",
          dialogIsShowCancel: false,
          dialogImgurl: "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/jiugong_pop_xiayilun@2x.png",
        });

      } else if (type == 11009) { //每个单词包最多学习三轮 
        /**
         * 弹框提示 三轮学习完了 去选择其他词库进行学习 点确定跳词库选择界面
         */
        that.setData({
          isShow: true,
          dialogType: 4,
          dialogContent: res.data.detail,
          dialogSureText: "选择词库",
          dialogCancelText: "休息一下",
          dialogIsShowCancel: true,
          dialogImgurl: "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/jiugong_pop_huanciku@2x.png",
        });
      }else if(type ==11019 ){
        that.setData({
          isShow: true,
          dialogType: 4,
          dialogContent: res.data.detail,
          dialogSureText: "选择词库",
          dialogCancelText: "休息一下",
          dialogIsShowCancel: true,
          dialogImgurl: "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/jiugong_pop_huanciku@2x.png",
        });
      }

    });
  },


  //点击start按钮
  clickStart: function () {

    // 请求接口，根据type值跳转对应的界面
    if (this.data.packageid == null || this.data.packageid == "") {
      wx.showToast({
        title: '请先选择词库',
        icon: 'none'
      })
      return;
    }
    this.getSelectData();

  },

  // 点击各宫格
  clickbox: function (e) {
    let boxNum = e.currentTarget.dataset.index;
    console.log(boxNum);
    // wx.navigateTo({
    //   url: '/pages/studyWord/everybox/everybox?boxNum=' + boxNum + "&packageid=" + this.data.packageid,
    // })
    if (boxNum == 1) {
      wx.navigateTo({
        url: '/pages/studyWord/everybox/everybox?boxNum=' + boxNum + "&packageid=" + this.data.packageid,
      })
    } else {
      if (this.data.studyModel != 2) {
        wx.navigateTo({
          url: '/pages/studyWord/everybox/everybox?boxNum=' + boxNum + "&packageid=" + this.data.packageid,
        })
      } else {
        wx.navigateTo({
          url: '/pages/studyWord/reviewWord/reviewWord?boxNum=' + boxNum + "&packageid=" + this.data.packageid,
        })
      }
    }

  },

  //点击最后一个宫格--绿色宫格
  clicklastbox: function () {
    wx.navigateTo({
      url: '/pages/studyWord/lastbox/lastbox?packageid=' + this.data.packageid,
    })
  },
  //点击对话框取消键
  clickDialogCancel: function () {
    let that = this;
    that.setData({
      isShow: false,
    });
    if (that.data.dialogType == 3 || that.data.dialogType == 4 || that.data.dialogType == 5) {
      wx.navigateBack({
        delta: 1
      })
    }

    if (that.data.dialogType == 6) {
      //跳选词界面
      wx.navigateTo({
        url: '/pages/studyWord/selectWord/selectWord?packageid=' + that.data.packageid + '&studyData=' + that.data.studydata,
      })
    }
  },
  //点击对话框确定键
  clickDialogSure: function () {
    let that = this;
    that.setData({
      isShow: false,
    });
    if (that.data.dialogType == 1) {
      //跳单词识记界面
      wx.navigateTo({
        url: '/pages/studyWord/memorizeWord/memorizeWord?packageid=' + this.data.packageid + '&studyData=' + that.data.studydata,
      })
    } else if (that.data.dialogType == 2) {
      //跳学后检测界面
      wx.navigateTo({
        url: '/pages/studyWord/checkWord/checkWord?packageid=' + this.data.packageid + '&studyData=' + that.data.studydata,
      })
    } else if (that.data.dialogType == 3) {
      that.startnext();
    } else if (that.data.dialogType == 4) {
      that.changeCourse();
    } else if (that.data.dialogType == 5) {

    } else if (that.data.dialogType == 6) {
      //跳转自主复习界面
      wx.navigateTo({
        url: '/pages/studyWord/independentreview/independentreview?packageid=' + this.data.packageid,
      })
    }
  },
  /**
   *更换词库
   */
  changeCourse: function () {
    wx.navigateTo({
      url: '/pages/studyWord/selectCourse/selectCourse',
    })
  },

  // 点击自主复习
  clickReview: function () {
    let that = this;
    httpUtils.getIndpendentreviewWords({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: that.data.packageid,
      page: 1,
    }).then((res) => {
      let type = res.data.type;
      that.data.studydata = JSON.stringify(res.data.to_study);
      if (type == 1) {
        //跳自主复习界面
        wx.navigateTo({
          url: '/pages/studyWord/independentreview/independentreview?packageid=' + this.data.packageid,
        })
      } else if (type == 11016) { //有未完成的学后检测，请先完成检测
        let num = res.data.to_study.length;
        that.setData({
          isShow: true,
          dialogType: 2,
          dialogContent: "您还有" + num + "个待检查的单词，点击去检查~",
          dialogSureText: "开始检查",
          dialogCancelText: "取消",
          dialogIsShowCancel: true,
          dialogImgurl: "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/jiugong_pop_daijiancha@2x.png",
        });

      } else if (type == 11017) { //有未完成的单词识记，请先完成识记
        that.setData({
          isShow: true,
          dialogType: 1,
          dialogContent: "您上次选择的一篇单词还没背完，先去背完它吧~",
          dialogSureText: "继续识记",
          dialogCancelText: "取消",
          dialogIsShowCancel: true,
          dialogImgurl: "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/jiugong_pop_weibeiwan@2x.png",
        });
      } else if (type == 11009) { //每个单词包最多学习三轮 
        /**
         * 弹框提示 三轮学习完了 去选择其他词库进行学习 点确定跳词库选择界面
         */
        that.setData({
          isShow: true,
          dialogType: 4,
          dialogContent: "此轮学习已结束，开始新的一轮学习吧~",
          dialogSureText: "选择词库",
          dialogCancelText: "休息一下",
          dialogIsShowCancel: true,
          dialogImgurl: "https://yxpmaterial.oss-cn-beijing.aliyuncs.com/jiugong_pop_huanciku@2x.png",
        });
      }
    });


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.packageid == null || this.data.packageid == "") {
      return;
    }
    let that = this;
    console.log("获取各盒子的状态")
    that.getBoxsStatus();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    let that = this;
    that.setData({
      animstate: 'paused'
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})